package cn.com.hhrcw.job;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.quartz.AdaptableJobFactory;
import org.springframework.scheduling.quartz.CronTriggerFactoryBean;
import org.springframework.scheduling.quartz.JobDetailFactoryBean;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

/**
 * @program: stdss-boot-parent
 * @author: JD
 * @create: 2020-07-23 15:19
 **/
@Configuration
public class AdminConfig {

    @Autowired
    AdaptableJobFactory jobFactory;

    @Bean("AdminJobDetail")
    public JobDetailFactoryBean adminDetailFactoryBean(@Qualifier("AdminResumeStatisticsTask") AdminResumeStatisticsTask myQuartzJob) {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setJobClass(myQuartzJob.getClass());
        return jobDetailFactoryBean;
    }

    @Bean("InterviewTaskBean")
    public JobDetailFactoryBean interviewBean(@Qualifier("InterviewTask") InterviewTask interviewTask) {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setJobClass(interviewTask.getClass());
        return jobDetailFactoryBean;
    }

    @Bean("IndustryJobDetail")
    public JobDetailFactoryBean industryDetailFactoryBean(@Qualifier("IndustryStatisticsTask") IndustryStatisticsTask myQuartzJob) {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setJobClass(myQuartzJob.getClass());
        return jobDetailFactoryBean;
    }

    @Bean("AreaJobDetail")
    public JobDetailFactoryBean areaDetailFactoryBean(@Qualifier("AreaStatisticsTask") AreaStatisticsTask myQuartzJob) {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setJobClass(myQuartzJob.getClass());
        return jobDetailFactoryBean;
    }

    @Bean("LogsJobDetail")
    public JobDetailFactoryBean logsDetailFactoryBean(@Qualifier("LogsTask") LogsTask logsTask) {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setJobClass(logsTask.getClass());
        return jobDetailFactoryBean;
    }

    @Bean("interviewTaskTrigger")
    public CronTriggerFactoryBean interviewTaskTrigger(@Qualifier("InterviewTaskBean") JobDetailFactoryBean logsJobDetail) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression("0 0 1 * * ?");
        triggerFactoryBean.setJobDetail(logsJobDetail.getObject());
        return triggerFactoryBean;
    }

    @Bean("LogsJobTrigger")
    public CronTriggerFactoryBean logsJobTrigger(@Qualifier("LogsJobDetail") JobDetailFactoryBean logsJobDetail) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression("0 0 0 * * ?");
        triggerFactoryBean.setJobDetail(logsJobDetail.getObject());
        return triggerFactoryBean;
    }

    @Bean("IndustryJobTrigger")
    public CronTriggerFactoryBean industryJobTrigger(@Qualifier("IndustryJobDetail") JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression("0 0/20 * * * ?");
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        return triggerFactoryBean;
    }

    @Bean("AdminJobTrigger")
    public CronTriggerFactoryBean adminJobTrigger(@Qualifier("AdminJobDetail") JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression("0 0 0/1 * * ?");
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        return triggerFactoryBean;
    }

    @Bean("AreaJobTrigger")
    public CronTriggerFactoryBean areaJobTrigger(@Qualifier("AreaJobDetail") JobDetailFactoryBean jobDetailFactoryBean) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression("0 0/10 * * * ?");
        triggerFactoryBean.setJobDetail(jobDetailFactoryBean.getObject());
        return triggerFactoryBean;
    }

    @Bean
    public JobDetailFactoryBean interviewStartTaskBean(InterviewStartTask interviewStartTask) {
        JobDetailFactoryBean jobDetailFactoryBean = new JobDetailFactoryBean();
        jobDetailFactoryBean.setDurability(true);
        jobDetailFactoryBean.setJobClass(interviewStartTask.getClass());
        return jobDetailFactoryBean;
    }

    /**
     * 面试开始任务
     *
     * @param
     * @return org.springframework.scheduling.quartz.CronTriggerFactoryBean
     */
    @Bean("InterviewStartTrigger")
    public CronTriggerFactoryBean interviewStartTrigger(@Qualifier("interviewStartTaskBean") JobDetailFactoryBean interviewStartTaskBean) {
        CronTriggerFactoryBean triggerFactoryBean = new CronTriggerFactoryBean();
        triggerFactoryBean.setCronExpression("0 0/1 * * * ?");
        triggerFactoryBean.setJobDetail(interviewStartTaskBean.getObject());
        return triggerFactoryBean;
    }

    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(@Qualifier("AdminJobTrigger") CronTriggerFactoryBean indirectFundsBean,
                                                     @Qualifier("IndustryJobTrigger") CronTriggerFactoryBean industryTrigger,
                                                     @Qualifier("AreaJobTrigger") CronTriggerFactoryBean areaTrigger,
                                                     @Qualifier("LogsJobTrigger") CronTriggerFactoryBean logsJobTrigger,
        //                                                     @Qualifier("CloseInterviewTaskTrigger") CronTriggerFactoryBean closeInterviewTaskTrigger,
                                                     @Qualifier("interviewTaskTrigger") CronTriggerFactoryBean bean,
                                                     @Qualifier("InterviewStartTrigger") CronTriggerFactoryBean interviewStartTrigger
    ) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();
        schedulerFactoryBean.setTriggers(indirectFundsBean.getObject(), industryTrigger.getObject(), areaTrigger.getObject(),
            logsJobTrigger.getObject(), interviewStartTrigger.getObject(),
            //                closeInterviewTaskTrigger.getObject(),
            bean.getObject());
        schedulerFactoryBean.setJobFactory(jobFactory);
        return schedulerFactoryBean;
    }
}
